<?php

require_once("login/src/Model/UserInfo.php");
require_once("login/src/Model/LoginValidation.php");
require_once("Database/Model/DatabaseConnection.php");
require_once("Database/Model/WorkoutExercisesDAL.php");
require_once("MyWorkouts/Model/MyWorkout.php");

class MyWorkoutDAL  
{

     /**
    *@var LoginValidation 
    **/
	private $loginValidation;

    /**
    *@var WorkoutExercisesDAL 
    **/
	private $workoutExercisesDAL;

	public function __construct()
	{
        $this->loginValidation = new LoginValidation();
		$this->workoutExercisesDAL = new WorkoutExercisesDAL();
	}


    /**
    *@param int 
    *@throws new exceptions
    **/
    public function DeleteWorkout($workoutID)
    {
        $sql = "DELETE FROM myworkouts WHERE `WorkoutID` = $workoutID";  

        $mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

        $stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE) 
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }

        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $mysqli->close();
        $this->workoutExercisesDAL->DeleteWorkoutExercise($workoutID);
    }

    /**
    *@param int
    *@throws new exceptions
    *@return workout / workout array() 
    **/
	public function getWorkouts($workoutID = null)
	{
        $userID = $this->loginValidation->getLoggedInUserID();

        if($workoutID != null)
        { 
            $sql = "SELECT WorkoutID, Name, Description, UserInfoID
                    FROM myworkouts
                    WHERE UserInfoID = $userID AND WorkoutID = $workoutID";
            $workout = $this->Workouts($sql);     
            if(isset($workout[0]))
            { 
                return $workout[0];
            }
            else
            {
                return;
            }
        }
        else
        {
            $sql = "SELECT `WorkoutID`, Name, Description, UserInfoID FROM myworkouts  WHERE UserInfoID = $userID";
		    return $this->Workouts($sql);
	    }
    }

     /**
    *@param string
    *@throws new exceptions
    *@return array() workout 
    **/
	private function Workouts($sql)
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());


		$stmt = $mysqli->prepare($sql);
        if ($stmt == FALSE)
        {
            throw new Exception("prepare of [$sql] failed " . $this->mysqli->error);
        }
        
        $result = $stmt->execute();
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $result = $stmt->bind_result($workoutID, $name, $description, $userInfoID);
        if ($result == FALSE) 
        {
            throw new Exception("execute of [$sql] failed " . $stmt->error);
        }

        $workoutList = array();

        while ($stmt->fetch()) 
        {
            $workoutList[] = MyWorkout::createWorkoutFromDatabase($workoutID, $name, $description, $userInfoID);
        }
        $mysqli->close();
        return $workoutList;
	}

     /**
    *@param MyWorkout
    *@param array() exercises
    *@param int array
    *@param int array
    *@param int array
    *@throws new exceptions
    *@return workout / array() workout 
    **/
	public function saveNewWorkout(MyWorkout $myWorkout, $exercises, $reps, $sets, $order) 
	{
		$mysqli = new mysqli(   DatabaseConnection::getDatabaseServer(), 
                                DatabaseConnection::getDatabaseUser(), 
                                DatabaseConnection::getdatabasePassword(), 
                                DatabaseConnection::getDatabaseName(), 
                                DatabaseConnection::getConnectionInt());

		$sql = "INSERT INTO myworkouts 
                        (
                                Name, 
                                Description, 
                                UserInfoID
                        ) 
                        VALUES(?, ?, ?)";

        $statement = $mysqli->prepare($sql);
        if ($statement === FALSE) 
        {
            throw new Exception("prepare of $sql failed " . $this->mysqli->error);
        }

	    $name = $myWorkout->getName();
        $description = $myWorkout->getDescription(); 
        $userID = $this->loginValidation->getLoggedInUserID();					

        if ($statement->bind_param("ssi", $name, 
                                           $description, 
                                           $userID) === FALSE) 
        {
            throw new Exception("bind_param of $sql failed " . $statement->error);
        }

        if ($statement->execute() === FALSE) 
        {
            throw new Exception("execute of $sql failed " . $statement->error);
        }
        $workoutID = $mysqli->insert_id;
		$mysqli->close();


		$this->saveNewWorkoutExercises($exercises, $reps, $sets, $order, $workoutID);
		
	}


     /**
    *@param exercise array
    *@param int array
    *@param int array
    *@param int array
    *@param int 
    **/
    private function saveNewWorkoutExercises($exercises,$reps, $sets, $order, $workoutID)
    {
        foreach($exercises as $ex)
        {
            $tempSetsNumber;
            $tempRepsNumber;
            $tempOrderNumber;
            foreach(array_keys($sets) as $temp)
            {
                if($temp == $ex)
                {
                    $tempSetsNumber = $sets[$temp];
                }
            }
            foreach(array_keys($reps) as $temp)
            {
                if($temp == $ex)
                {
                    $tempRepsNumber = $reps[$temp];
                }
            }
            foreach(array_keys($order) as $temp)
            {   
                if($temp == $ex)
                {   
                    $tempOrderNumber = $order[$temp];
                }
            }
            

            $this->workoutExercisesDAL->saveNewWorkoutExercise($workoutID,
                                                               $ex, 
                                                               $tempSetsNumber,
                                                               $tempRepsNumber,
                                                               $tempOrderNumber);
        }
    }

}






